/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "矢量计算.h"

/*Mat44 LU(const Mat44& m, Mat44& L)
{
	Mat44 U = m;
	L = Mat44::kIdentity;

	// for each row
	for (int j = 0; j < 4; ++j)
	{
		Mat44 Li, LiInv;
		Li = Mat44::kIdentity;
		LiInv = Mat44::kIdentity;

		float32 pivot = U(j, j);

		if (pivot == 0.0)
			return U;

		assert(pivot != 0.0);

		// zero our all entries below pivot
		for (int i = j + 1; i < 4; ++i)
		{
			Mat44 l = -U(i, j) / pivot;

			Li(i, j) = l;

			// create inverse of L1..Ln as we go (this is L)
			L(i, j) = -l;
		}

		U = Li * U;
	}

	return U;
}*/


/*Mat44 Determinant(const Mat44& A, Mat44& L, Mat44& U)
{
	U = LU(A, L);

	// determinant is the product of diagonal entries of U (assume L has 1s on diagonal)
	T det = 1.0;
	for (int i = 0; i < n; ++i)
		det *= U(i, i);

	return det;
}



Mat44 Inverse(const Mat44& A, T& det)
{
	Mat44 L, U;
	det = Determinant(A, L, U);

	XMatrix<n, n> Inv;

	if (det != 0.0f)
	{
		for (int i = 0; i < n; ++i)
		{
			// solve for each column of the identity matrix
			XMatrix<n, 1> I;
			I(i, 0) = 1.0;

			XMatrix<n, 1> x = Solve(L, U, I);

			Inv.SetCol(i, x);
		}
	}

	return Inv;
}*/


CUDA_CALLABLE std::vector<vec3> vec_Mul(const std::vector<vec3>& sv, const float32 s)
{
	uint32 num = sv.size();
	auto* vert = sv.data();

	CUDA_CALLABLE std::vector<vec3> r(num);
	auto* r_p = r.data();
	for (uint32 i = 0; i < num; ++i) {
		r_p[i].x = vert[i].x * s;
		r_p[i].y = vert[i].y * s;
		r_p[i].z = vert[i].z * s;
	}

	return r;
}

CUDA_CALLABLE std::vector<vec3> vec_Mul(const std::vector<vec4>& sv, const float32 s)
{
	uint32 num = sv.size();
	auto* vert = sv.data();

	CUDA_CALLABLE std::vector<vec3> r(num);
	auto* r_p = r.data();
	for (uint32 i = 0; i < num; ++i) {
		r_p[i].x = vert[i].x * s;
		r_p[i].y = vert[i].y * s;
		r_p[i].z = vert[i].z * s;
	}

	return r;
}

